//
//  GRCertificationHeader.m
//  YC_GRice
//
//  Created by LiuFeng on 2018/7/21.
//  Copyright © 2018年 YeCai. All rights reserved.
//

#import "GRCertificationHeader.h"
#import "PhotoBrowerManager.h"

@implementation GRCertificationHeader

- (instancetype)initWithCertification:(GRCertificationType)type {
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self drawUIWithCertification:type];
    }
    return self;
}


- (void)drawUIWithCertification:(GRCertificationType)type {
    switch (type) {
        case GRCertificationSafety:{
            self.frame = CGRectMake(0, 0, KscreenWidth, 215);
            CGFloat hy = [self drawOnlyTitle:@"安全认证" prompt:nil];
            [self certification:GRCertificationSafety atHY:hy];
        }
            break;
            
        case GRCertificationPersonal:{
            self.frame = CGRectMake(0, 0, KscreenWidth, 90);
            [self drawOnlyTitle:@"个人认证" prompt:@"所有资料严格保密，不会向第三方透露"];
        }
            break;
            
        case GRCertificationIdCard:{
            self.frame = CGRectMake(0, 0, KscreenWidth, 230);
            CGFloat hy = [self drawOnlyTitle:@"身份证认证" prompt:@"请填写本人真实有效信息"];
            [self certification:GRCertificationIdCard atHY:hy];

        }
            break;

        case GRCertificationBankCard:{
            self.frame = CGRectMake(0, 0, KscreenWidth, 230);
            CGFloat hy = [self drawOnlyTitle:@"银行卡认证" prompt:@"请绑定持卡人本人的银行卡号"];
            [self certification:GRCertificationBankCard atHY:hy];
        }
            break;

        default:{
            self.frame = CGRectMake(0, 0, KscreenWidth, 90);
            [self drawOnlyTitle:@"联系人认证" prompt:@"非紧急情况下不会联系，所有资料严格保密"];
        }
            break;
    }
}

- (CGFloat)drawOnlyTitle:(NSString *)title prompt:(NSString *)prompt {
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, 15, self.frame.size.width-16, 40)];
    titleLabel.text       = title;
    titleLabel.font       = [UIFont boldSystemFontOfSize:25];
    titleLabel.textColor  = KGMBLACK_COLOR;
    [self addSubview:titleLabel];

    CGFloat hy = 55;
    if (prompt) {
        UILabel *promptLabel  = [[UILabel alloc] initWithFrame:CGRectMake(16, hy, self.frame.size.width-16, 20)];
        promptLabel.font      = [UIFont systemFontOfSize:15];
        promptLabel.text      = prompt;
        promptLabel.textColor = HEXCOLOR(0x999999);
        [self addSubview:promptLabel];
        hy = 90;
    }
    
    return hy;
}

- (void)certification:(GRCertificationType)type atHY:(CGFloat)hy {
    switch (type) {
        case GRCertificationSafety:{
            [self drawSafety:hy];
        }
            break;
          
        case GRCertificationIdCard:{
            UIView *leftView = [self createUIViewWithFrame:CGRectMake(15, hy+10, (self.frame.size.width-45)/2, 120) image:[UIImage imageNamed:@"usercenter_20"] text:@"拍摄人像页" SEL:@selector(photographTheFrontOfIdcard:)];
            [self addSubview:leftView];
            
            UIView *rightView = [self createUIViewWithFrame:CGRectMake(30 + leftView.width, hy+10, leftView.width, 120) image:[UIImage imageNamed:@"usercenter_21"] text:@"拍摄国徽页" SEL:@selector(photographTheBackOfIdcard:)];
            [self addSubview:rightView];
        }
            break;
            
        case GRCertificationBankCard:{
            UIView *bankView = [self createUIViewWithFrame:CGRectMake(self.frame.size.width/4, hy+10, self.frame.size.width/2, 120) image:[UIImage imageNamed:@"usercenter_24"] text:@"拍摄银行卡正面" SEL:@selector(shootingTheBank:)];
            [self addSubview:bankView];
        }
            break;
            
        default:
            break;
    }
}

#pragma Mark ==================    方法   ======================

// 拍摄身份证正面
- (void)photographTheFrontOfIdcard:(UIButton *)sender {
    UIImageView *imageview = [sender.superview viewWithTag:100];
    [self photographIDCard:YES IMGView:imageview];
}

// 拍摄身份证反面
- (void)photographTheBackOfIdcard:(UIButton *)sender {
    UIImageView *imageview = [sender.superview viewWithTag:100];
    [self photographIDCard:NO IMGView:imageview];
}

- (void)photographIDCard:(BOOL)isFront IMGView:(UIImageView *)imageview {
    WEAKSELF
    [self getShootingIMG:^(NSString *imageUrl) {
        NSString *imgUrl = [NSString stringWithFormat:@"%@?imageslim/thumbnail/!50p",imageUrl];
        [GRCertificationAction identifyIDCardWithDirection:isFront IMGURL:imgUrl completionHandler:^(BOOL success, id responseObject, GRError *error) {
            if (success) {
                [imageview sd_setImageWithURL:[NSURL URLWithString:imgUrl]];
                NSString *key     = isFront ? @"idCard" : @"idCardBack";
                NSMutableDictionary *param = [NSMutableDictionary dictionaryWithDictionary:responseObject];
                [param setValue:imgUrl forKey:key];
                if ([weakSelf.delegate respondsToSelector:@selector(getIdentificationInfomation:)]) {
                    [weakSelf.delegate getIdentificationInfomation:param];
                }
            }else {
                [GRHUD showErrorMsg:[error errorMsg]];
            }
            [GRHUD dismissHUD];
        }];
    }];
}


// 拍摄银行卡
- (void)shootingTheBank:(UIButton *)sender {
    UIImageView *imageview = [sender.superview viewWithTag:100];
    WEAKSELF
    [self getShootingIMG:^(NSString *imageUrl) {
        NSString *imgUrl = [NSString stringWithFormat:@"%@?imageslim/thumbnail/!50p",imageUrl];
        [GRCertificationAction identifyBankCardWithIMGURL:imgUrl completionHandler:^(BOOL success, id responseObject, GRError *error) {
            if (success) {
                [imageview sd_setImageWithURL:[NSURL URLWithString:imgUrl]];
                NSMutableDictionary *param = [NSMutableDictionary dictionaryWithDictionary:responseObject];
                [param setValue:imgUrl forKey:@"bankImg"];
                if ([weakSelf.delegate respondsToSelector:@selector(getIdentificationInfomation:)]) {
                    [weakSelf.delegate getIdentificationInfomation:param];
                }
            }else {
                [GRHUD showErrorMsg:[error errorMsg]];
            }
            [GRHUD dismissHUD];
        }];
    }];
}


/**
 上传拍照图片

 @param completion <#completion description#>
 */
- (void)getShootingIMG:(void(^)(NSString *imageUrl))completion {
    [[PhotoBrowerManager defaultManager] multipleImages:1 ContentMode:PhotoBrowserContentModeCamera CurrentVc:self.currentVc FinishBlock:^(NSArray<UIImage *> *images) {
        [GRHUD showProgress:@"识别中"];
        [[GRUploadManager defaultManager] uploadSingleIMAGE:images.firstObject completion:^(NSString *imgURL, BOOL success) {
            completion(imgURL);
        }];
    }];
}





#pragma Mark ==================  绘制视图  =====================

- (void)drawSafety:(CGFloat)hy {
    UIView *safeview = [[UIView alloc] initWithFrame:CGRectMake(0, hy, self.frame.size.width, 160)];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.frame.size.width/2-37.5, 0, 75, 90)];
    imageView.image        = [UIImage imageNamed:@"usercenter_15"];
    [safeview addSubview:imageView];
    
    UILabel *safetop      = [[UILabel alloc] initWithFrame:CGRectMake(15, 95, self.frame.size.width-30, 20)];
    safetop.text          = @"为了保障您的账户安全，需完成以下认证";
    safetop.font          = [UIFont boldSystemFontOfSize:15];
    safetop.textColor     = KGMBLACK_COLOR;
    safetop.textAlignment = NSTextAlignmentCenter;
    [safeview addSubview:safetop];
    
    UILabel *safeBottom      = [[UILabel alloc] initWithFrame:CGRectMake(15, 115, self.frame.size.width-30, 20)];
    safeBottom.text          = @"我们承诺保护您的信息安全";
    safeBottom.font          = [UIFont systemFontOfSize:15];
    safeBottom.textColor     = HEXCOLOR(0x999999);
    safeBottom.textAlignment = NSTextAlignmentCenter;
    [safeview addSubview:safeBottom];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 150, self.frame.size.width, 10)];
    lineView.backgroundColor = HEXCOLOR(0xf8f8f8);
    [safeview addSubview:lineView];
    
    [self addSubview:safeview];
}

- (UIView *)createUIViewWithFrame:(CGRect)frame image:(UIImage *)image text:(NSString *)text SEL:(SEL)selector {
    UIView *view = [[UIView alloc] initWithFrame:frame];
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height-15)];
    imageView.image = image;
    imageView.tag   = 100;
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    [view addSubview:imageView];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(frame.size.width/2-17.5, 25, 35, 35);
    [button setImage:[UIImage imageNamed:@"usercenter_22"] forState:UIControlStateNormal];
    [button addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:button];
    
    UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, button.frame.origin.y + button.frame.size.height + 8, frame.size.width-20, 15)];
    textLabel.font = [UIFont boldSystemFontOfSize:14];
    textLabel.text = text;
    textLabel.textAlignment = NSTextAlignmentCenter;
    textLabel.textColor = KGMBLACK_COLOR;
    [view addSubview:textLabel];
    
    return view;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
